--
-- @Author:      name
-- @DateTime:    2018-8-21 17:03:10
-- @Description: 消息的处理

local skynet = require "skynet"
local log = require "Log"
local CommandBase = require ".common.CommandBase"
require "common"

local AssertEx = AssertEx
local Command = class("Command",CommandBase)
---------------------------------------------------------
-- Private
---------------------------------------------------------
function Command:ctor(message,machine)
	self.super:ctor(message,machine)
	self.msg = message 
	self.machine = machine 
	self.close  = false
	self.tableid = self.machine:getTableId()
	self.gameid = getGameIdbyTable(self.tableid)
	self:register()
end

--注册本服务里的消息
function Command:register()
	self.msg:registerSelf('zfb_status_req',handler(self,self.getStatus))
	self.msg:registerSelf('zfb_bet_req',handler(self,self.bet))
	self.msg:registerSelf('zfb_rebet_req',handler(self,self.rebet))

	self.msg:registerSelf('zfb_shangzhuang_req',handler(self,self.shangzhuang)) 
	self.msg:registerSelf('zfb_remove_zhuang_list_req',handler(self,self.removeShangzhuangList))
	self.msg:registerSelf('zfb_xiazhuang_req',handler(self,self.xiazhuang))
	self.msg:registerSelf('zfb_shangzhuang_list_req',handler(self,self.getZhuangList))
	self.msg:registerSelf('zfb_history_req',handler(self,self.getHistoryList))
	self.msg:registerSelf('zfb_no_seat_player_list_req',handler(self,self.getNoSeatPlayerList))
	self.msg:registerSelf('zfb_player_req',handler(self,self.getPlayerInfo))

	self.msg:registerSelf('enter_table_req',handler(self,self.enterTable))
	self.msg:registerSelf('leave_table_req',handler(self,self.leaveTable))
end

function Command:enterTable(playerid,playerInfo)
	local res = self.super.enterTable(self, playerid, playerInfo)	
	local sql = string.format("replace into user_online (uid, sid, game, `leave`) values(%d, %d, %d, %d);", playerid, self.gameid, self.gameid, 2)
	skynet.call("mysql_service", "lua", "execute", sql)	

	skynet.fork(function ()
		skynet.sleep(30)
		local status = self.machine:getTableStatus(playerid)
		self.machine:sendMessage(playerid, {zfb_status_res = status})
		self.machine:ntNoSeatPlayerNum(playerid)
	end)
	print("-------------------------------------->enterTable res", res)
	return res
end

function Command:leaveTable(playerid,playerInfo)
	local res = self.super.leaveTable(self, playerid, playerInfo)	
	local sql = string.format("delete from user_online where uid = %d;", playerid)
	skynet.call("mysql_service", "lua", "execute", sql)
	skynet.fork(function ()
		self.machine:ntNoSeatPlayerNum(playerid)
	end)
	return res
end
---------------------------------------------------------
-- REQUEST
---------------------------------------------------------

-- 得到桌子状态
function Command:getStatus(playerid)
	local player = self.machine:getPlayer(playerid)
	AssertEx(player, 'zfb_status_res', 10)
	--重连进入
	player.disconnect = false
	local status = self.machine:getTableStatus(playerid)
	return {zfb_status_res = status}
end

------------------------------------------------------------------------------------------------
---游戏里的操作
------------------------------------------------------------------------------------------------
--下注
function Command:bet(playerid, args)
	--print("_________Command:bet", args,  'playerid:' ..playerid)
	local player = self.machine:getPlayer(playerid)
	AssertEx(player, 'zfb_bet_res', 10)	
	local res = self.machine:onPlayerBet(playerid,args)
	if not res then
		return
	end
	return {zfb_bet_res = res}	
end

--重复下注
function Command:rebet(playerid, args)
	local player = self.machine:getPlayer(playerid)
	AssertEx(player, 'zfb_rebet_res', 10)	
	local res = self.machine:onPlayerRebet(playerid)
	--print("+++++++++++++++++++++++++> res",res)
	if not res then 
		return 
	end
	return {zfb_rebet_res = res}	
end

--上庄
function Command:shangzhuang(playerid, args)
	local player = self.machine:getPlayer(playerid)
	AssertEx(player, 'zfb_shangzhuang_res', 10)	
	local res = self.machine:onPlayerShangzhuang(playerid, args)
	if not res then 
		return 
	end
	return {zfb_shangzhuang_res = res}	
end

--退出上庄队列
function Command:removeShangzhuangList(playerid, args)
	local player = self.machine:getPlayer(playerid)
	AssertEx(player, 'zfb_remove_shangzhuang_list_res', 10)	
	local res = self.machine:onPlayerRemoveZhuangList(playerid)
	if not res then 
		return 
	end
	return {zfb_remove_zhuang_list_res = res}	
end

--下庄
function Command:xiazhuang(playerid, args)
	local player = self.machine:getPlayer(playerid)
	AssertEx(player, 'zfb_xiazhuang_res', 10)	
	local res = self.machine:onPlayerXiazhuang(playerid)
	--print("+++++++++++++++++++++++++> res",res)
	if not res then 
		return 
	end
	return {zfb_xiazhuang_res = res}	
end

--获取上庄列表
function Command:getZhuangList(playerid, args)
	local player = self.machine:getPlayer(playerid)
	AssertEx(player, 'zfb_shangzhuang_list_res', 10)	
	local res = self.machine:getZhuangList(args)
	--print("+++++++++++++++++++++++++> res",res)
	res = {shangzhuang_list = res} 
	return {zfb_shangzhuang_list_res = res}	
end

--获取历史列表
function Command:getHistoryList(playerid, args)
	local player = self.machine:getPlayer(playerid)
	AssertEx(player, 'zfb_history_res', 10)	
	local res = self.machine:getHistoryList(args)
	if not res then 
		return 
	end
	return {zfb_history_res = res}	
end

--获取无座列表
function Command:getNoSeatPlayerList(playerid, args)
	local player = self.machine:getPlayer(playerid)
	AssertEx(player, 'zfb_no_seat_player_list_res', 10)	
	local res = self.machine:getNoSeatPlayerList(args)
	if not res then 
		return 
	end
	return {zfb_no_seat_player_list_res = res}	
end

function Command:getPlayerInfo(playerid, args)
	local player = self.machine:getPlayer(playerid)
	AssertEx(player, 'zfb_no_seat_player_list_res', 10)	
	local resPlayer = self.machine:getPlayerInfo(playerid)
	return {zfb_player_res = {player = resPlayer}}	
 end 



return Command